package cn.luischen.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.luischen.constant.Types;
import cn.luischen.constant.WebConst;
import cn.luischen.model.UserDomain;
import cn.luischen.utils.IPKit;
import cn.luischen.utils.MapCache;
import cn.luischen.utils.TaleUtils;
import cn.luischen.utils.UUID;

/**
 * 自定义拦截器
 */
@Component
public class BaseInterceptor2 implements HandlerInterceptor {
	private static final Logger LOGGE = LoggerFactory.getLogger(BaseInterceptor2.class);
	private static final String USER_AGENT = "user-agent";

	private final MapCache cache = MapCache.single();

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
		String uri = request.getRequestURI();

		LOGGE.info("UserAgent: {}", request.getHeader(USER_AGENT));
		LOGGE.info("用户访问地址: {}, 来路地址: {}", uri, IPKit.getIpAddrByRequest(request));

		// 请求拦截处理
		UserDomain user = TaleUtils.getLoginUser(request);
		if (null == user) {
			Integer uid = TaleUtils.getCookieUid(request);
			if (null != uid) {
				// 这里还是有安全隐患,cookie是可以伪造的

				request.getSession().setAttribute(WebConst.LOGIN_SESSION_KEY, user);
			}
		}
		if (uri.startsWith("/admin") && !uri.startsWith("/admin/login") && null == user && !uri.startsWith("/admin/css")
				&& !uri.startsWith("/admin/images") && !uri.startsWith("/admin/js") && !uri.startsWith("/admin/plugins")
				&& !uri.startsWith("/admin/editormd")) {
			response.sendRedirect(request.getContextPath() + "/admin/login");
			return false;
		}
		// 设置get请求的token
		if (request.getMethod().equals("GET")) {
			String csrf_token = UUID.UU64();
			// 默认存储30分钟
			cache.hset(Types.CSRF_TOKEN.getType(), csrf_token, uri, 30 * 60);
			request.setAttribute("_csrf_token", csrf_token);
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
			ModelAndView modelAndView) throws Exception {
		httpServletRequest.setAttribute("commons", "commons");// 一些工具类和公共方法
		httpServletRequest.setAttribute("option", "option");
		httpServletRequest.setAttribute("adminCommons", "adminCommons");
		initSiteConfig(httpServletRequest);

		int i = 1;
		i = getNumber(i);
		initSiteConfig(httpServletRequest);
		initSiteConfig2(httpServletRequest);
		initSiteConfig3(httpServletRequest);
		initSiteConfig4(httpServletRequest);
	}

	public int getNumber(int i) {
		return i + 1;
	}

	private void initSiteConfig(HttpServletRequest request) {
		if (WebConst.initConfig.isEmpty()) {
			Map<String, String> querys = new HashMap<>();

			WebConst.initConfig = querys;
		}
	}

	private static void initSiteConfig2(HttpServletRequest request) {
		LOGGE.info("BaseInterceptor.initSiteConfig2");
	}

	public static void initSiteConfig3(HttpServletRequest request) {
		LOGGE.info("BaseInterceptor.initSiteConfig3");
	}

	public void initSiteConfig4(HttpServletRequest request) {
		LOGGE.info("BaseInterceptor.initSiteConfig4");
	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Object o, Exception e) throws Exception {

	}
}
